<?php
namespace app\common\model;
use think\Validate;

class AdvertPosition extends Common
{
    protected $autoWriteTimestamp = true;
    protected $createTime = 'ctime';
    protected $updateTime = 'utime';

    protected $rule = [
        'name' => 'require|max:50',
        'sort' => 'number',
    ];

    protected $msg = [
        'name.require' => '名称必须',
        'name.max' => '名称最多不能超过50个字符',
        'sort.number' => '排序只能是数字格式',
    ];


    /**
     * @param $post
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function tableData($post)
    {
        if(isset($post['limit']))
        {
            $limit = $post['limit'];
        }
        else
        {
            $limit = config('paginate.list_rows');
        }
        $tableWhere = $this->tableWhere($post);

        $list = $this->field($tableWhere['field'])
            ->where($tableWhere['where'])
            ->order($tableWhere['order'])
            ->paginate($limit);
        $data = $this->tableFormat($list->getCollection()); //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = $list->total();
        $re['data'] = $data;
        return $re;
    }


    /**
     * 添加广告位
     * @param $data
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addData($data)
    {
        $result = [
            'status' => true,
            'msg' => '保存成功',
            'data' => ''
        ];
        $validate = new Validate($this->rule,$this->msg);
        //验证
        if(!$validate->check($data))
        {
            $result['status'] = false;
            $result['msg'] = $validate->getError();
        }
        else
        {
            //判断商户该模板是否已经添加
            if($this->where('code', $data['code'])->find())
            {
                $result['status'] = false;
                $result['msg'] = '该广告位模板已经添加';
            }
            else
            {
                //写入数据
                if(!$this->allowField(true)->save($data))
                {
                    $result['status'] = false;
                    $result['msg'] = '保存失败';
                }
            }
        }
        return $result;
    }


    /**
     * 广告位编辑更新
     * @param $data
     * @return array
     */
    public function saveData($data)
    {
        $validate = new Validate($this->rule,$this->msg);
        $result = [
            'status' => true,
            'msg' => '保存成功',
            'data' => ''
        ];
        if(!$validate->check($data))
        {
            $result['status'] = false;
            $result['msg'] = $validate->getError();
        }
        else
        {
            if(!$this->allowField(true)->save($data, ['id' => $data['id']]))
            {
                $result['status'] = false;
                $result['msg'] = '保存失败';
            }
        }
        return $result;
    }


    /**
     * 广告位删除
     * @param int $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function del($id = 0)
    {
        //广告位下有广告禁止删除
        $result = [
            'status' => true,
            'msg' => '删除成功',
            'data'=>''
        ];
        if($this->advert()->where('id', $id)->find())
        {
            $result['status'] = false;
            $result['msg'] = '该广告位下有广告删除失败';
        }
        else
        {
            if(!$this->where('id', $id)->delete())
            {
                $result['status'] = false;
                $result['msg'] = '删除失败';
            }
        }
        return $result;
    }


    /**
     * 根据查询结果，格式化数据
     * @param $list
     * @return mixed
     */
    protected function tableFormat($list)
    {
        foreach($list as $key => $val)
        {
            $list[$key]['ctime'] = getTime($val['ctime']);
            $list[$key]['utime'] = getTime($val['utime']);
        }
        return $list;
    }


    /**
     * where 搜索条件
     * @param $post
     * @return mixed
     */
    protected function tableWhere($post)
    {
        $where = [];
        if(isset($post['name']) && $post['name'] != "")
        {
            $where[] = ['name', 'eq', $post['name']];
        }
        if(isset($post['utime']) && $post['utime'] != "")
        {
            $date_array = explode('~', $post['utime']);
            $sutime = strtotime($date_array[0].'00:00:00', time());
            $eutime = strtotime($date_array[1].'23:59:59', time());
            $where[] = ['utime', ['EGT', $sutime], ['ELT', $eutime], 'and'];
        }
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = ['sort ASC'];
        return $result;
    }



    /**
     * 广告位 和 广告 一对多关联
     * @return \think\model\relation\HasMany
     */
    public function advert()
    {
        return $this->hasMany('advertisement', 'position_id', 'id');
    }

}